package com.ysyx.ysyx_driverinfo.controller;

import com.ysyx.ysyx_driverinfo.dto.BaseDto;
import com.ysyx.ysyx_driverinfo.pojo.DriverPojo;
import com.ysyx.ysyx_driverinfo.qo.DriverQo;
import com.ysyx.ysyx_driverinfo.qo.GeoQo;
import com.ysyx.ysyx_driverinfo.service.DriverPojoService;
import com.ysyx.ysyx_driverinfo.util.Sample;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 *
 * @description: xxxx接口/类
 * @author:郭宇轩
 * @create:2023/4/7 19:27
 */
@RestController
@RequestMapping("/driver")
public class DriverController {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private DriverPojoService driverPojoService;

    /*
     * @Description: (作用) 司机账号登录
     * @params: String driverAcc,String driverPwd
     * @return: statusCode 0程序错误 101登录成功司机已有注册信息,102 司机登录成功司机没有完善注册信息,driverPojo 201账号不存在 301账号或密码错误
     * @Author: 郭宇轩
     * @create:2023/4/8
     */
    @RequestMapping(value = "/acclogin",method = RequestMethod.GET)
    public BaseDto acclogin(String driverAcc, String driverPwd){

        System.out.println("acc"+driverAcc+",pwd"+driverPwd);

        BaseDto baseDto = driverPojoService.login(driverAcc, driverPwd);
        return baseDto;
    }

    /*
     * @Description: (作用) 司机手机登录
     * @params: String driverTel,String code
     * @return:
     * @Author: 郭宇轩
     * @create:2023/4/8
     */
    @RequestMapping(value = "/tellogin",method = RequestMethod.POST)
    public BaseDto tellogin(String driverTel,String code){
        return new BaseDto(0,driverTel+code,null);
    }




    /*
     * @Description: (作用)
     * @params: 地理位置经纬度laitude 司机id driverid
     * @return: baseDto 上工成功101
     * @Author: 薛宇晖
     * @create:2023/4/12
     */
    @RequestMapping(value = "/upWork",method = RequestMethod.POST)
    public BaseDto driverUpwork(@RequestBody GeoQo qo){
        //添加司机的位置信息到地理桶
        return driverPojoService.driverUpwork(qo);
    }

    /*
     * @Description: (作用)
     * @params: Integer driverId
     * @return: BaseDto(0,null,list[0]是司机Vo对象  )
     * @Author: 郭宇轩
     * @create:2023/4/13
     */
    @RequestMapping(value = "/{driverId}",method = RequestMethod.GET)
    public BaseDto selectOne(@PathVariable Integer driverId){
        System.out.println("driverId=="+driverId);
        BaseDto baseDto = driverPojoService.getDriverInfo(driverId);
        return baseDto;
    }

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public BaseDto selectAllDriverDetail(){
        BaseDto baseDto = driverPojoService.selectAllDriverDetail();
        return baseDto;
    }




    @RequestMapping(value = "/reg/getCode/{tel}",method = RequestMethod.GET)
    public void getCheckCode(@PathVariable String tel) throws Exception {
        System.err.println("打印"+tel);
//        java.util.List<String> args = java.util.Arrays.asList(args_);
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = Sample.createClient("LTAI5tEB6gJYarMLG5FaR7Ks", "obHaxsxSOPLg5NexwTkXaQYMPhDBWn");

        //生成四位随机数
        String x;//定义两变量

        Random ne=new Random();//实例化一个random的对象ne

        x= String.valueOf(ne.nextInt(9999-1000+1)+1000);//为变量赋随机值1000-9999

        System.out.println("产生的随机数是:"+x);//输出

        //将生成的随机数存在redis中
        RBucket<Object> bucket = redissonClient.getBucket("reg:code:" + tel);
        bucket.set(x);
        bucket.expire(300, TimeUnit.SECONDS);


//        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
//                .setSignName("云上易行")
//                .setTemplateCode("SMS_275410540")
//                .setPhoneNumbers("15060186858")
//                .setTemplateParam("{\"code\":\""+x+"\"}");
//        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        try {
//            // 复制代码运行请自行打印 API 的返回值
//            client.sendSmsWithOptions(sendSmsRequest, runtime);
//        } catch (TeaException error) {
//            // 如有需要，请打印 error
//            com.aliyun.teautil.Common.assertAsString(error.message);
//        } catch (Exception _error) {
//            TeaException error = new TeaException(_error.getMessage(), _error);
//            // 如有需要，请打印 error
//            com.aliyun.teautil.Common.assertAsString(error.message);
//        }
    }


    @RequestMapping(value = "/reg")
    public BaseDto reg(@RequestBody DriverQo qo){
        BaseDto baseDto = driverPojoService.DriverReg(qo);
        return baseDto;

    }

    @RequestMapping(value = "/checkCode/{code}/{tel}",method = RequestMethod.GET)
    public BaseDto checkCode(@PathVariable String code,@PathVariable String tel){
        RBucket<String> bucket = redissonClient.getBucket("reg:code:" + tel);
        String s = bucket.get();
        if (s.equals(code)){
            return new BaseDto(101,"可以跳转",null);
        }else {
            return new BaseDto(201,"失败",null);
        }
    }

    /*
     * @Description: (作用)司机查询所有订单
     * @params: Integer driverId
     * @return:  BaseDto(0,null,list为订单list)
     * @Author: 郭宇轩
     * @create:2023/4/21
     */
    @RequestMapping(value = "/selectAllOrder",method = RequestMethod.GET)
    public BaseDto selectAllOrder(Integer driverId){
        BaseDto baseDto = driverPojoService.selectAllOrder(driverId);
        return baseDto;
    }

    @RequestMapping("/fixPwd")
    public BaseDto fixPwd(@RequestBody DriverPojo driverPojo){

        return driverPojoService.fixPwd(driverPojo);
    }

}
